package vn.mobiistar.videoapp.other;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.activity.MainActivity;
import vn.mobiistar.videoapp.fragment.SimpleVideoFragment;
import vn.mobiistar.videoapp.fragment.VideoDetailFragment;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Surface;
import android.widget.Toast;

import com.github.pedrovgs.DraggableListener;
import com.github.pedrovgs.DraggablePanel;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;

public class VideoPlayer {
	private static final String TAG = "VideoPlayer";
	private YouTubePlayerSupportFragment mYoutubeFragment;
//	private SimpleVideoFragment mYoutubeFragment;
	private YouTubePlayer 				 mYoutubePlayer;
	private Fragment 				 	 mFragment;
	private DraggablePanel 				 mDraggablePanel;
	
	private Fragment 					 mFragmentDetail;
	private Video 						 mVideo;
	private Context 					 mContext;
	public boolean						 mIsInitiated = false;
	
	public VideoPlayer(Context context, Fragment fragment, DraggablePanel draggablePanel){
		mDraggablePanel  = draggablePanel;
		mFragment        = fragment;
		mContext         = context;
	}
	public DraggablePanel getDraggablePanel() {
		return mDraggablePanel;
	}
	public void setDraggablePanel(DraggablePanel mDraggablePanel) {
		this.mDraggablePanel = mDraggablePanel;
	}
	public YouTubePlayer getYoutubePlayer() {
		return mYoutubePlayer;
	}
	public void playVideo(final Video video,final int rotation,int delay){
		mVideo = video;
		Log.i(TAG, "Play video at display rotation: "+rotation);
		// neu la lan dau chay
		if(!mIsInitiated){
			initialize(rotation);
			mIsInitiated = true;
		}else{
			mYoutubePlayer.pause();
			mDraggablePanel.maximize();
			// bat buoc phai post delay de tranh loi draggble view
			mDraggablePanel.postDelayed(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Log.i(TAG, " updating player draggble panel ");
					loadVideoId(video, rotation);
					((VideoDetailFragment) mFragmentDetail).init(mVideo,false);
				}
			},delay);
			
		}
	}
	private void loadVideoId(Video video,int rotation){
		if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270 ) {
			mYoutubePlayer.setFullscreen(true);
		}
		if(mYoutubePlayer!= null)mYoutubePlayer.loadVideo(mVideo.getVideoid());
		else Toast.makeText(mContext, mContext.getResources().getString(R.string.error_video_player), Toast.LENGTH_SHORT).show();
	}
	public boolean isPlaying(){
		return mYoutubePlayer.isPlaying();
	}
	public void maximize(){
		mDraggablePanel.maximize();
	}
	public void minimize(){
		mDraggablePanel.minimize();
	}
	private void initialize(int rotation){
		initializemYoutubeFragment(rotation);
		initializeDraggablePanel();
	}
	
	private void initializemYoutubeFragment(final int rotation) {
		mYoutubeFragment = new YouTubePlayerSupportFragment();
		mYoutubeFragment.initialize(Define.DEVELOPER_KEY,
				new YouTubePlayer.OnInitializedListener() {
					@Override
					public void onInitializationSuccess(
							YouTubePlayer.Provider provider,
							YouTubePlayer player, boolean wasRestored) {
						if (!wasRestored) {
							mYoutubePlayer = player;
							loadVideoId(mVideo, rotation);
						}
					}
					@Override
					public void onInitializationFailure(
							YouTubePlayer.Provider provider,
							YouTubeInitializationResult error) {
					}
				});
	}

	private void initializeDraggablePanel() {
		mDraggablePanel.setFragmentManager(mFragment.getFragmentManager());
		mDraggablePanel.setTopFragment((Fragment)mYoutubeFragment);
		mFragmentDetail = new VideoDetailFragment(mContext,mVideo,this);
		mDraggablePanel.setBottomFragment(mFragmentDetail);
		hookDraggablePanelListeners();
		mDraggablePanel.initializeView();
	}

	 //**
	 //* Hook the DraggableListener to DraggablePanel to pause or resume the video
	 //* when the DragglabePanel is maximized or closed.
	 //*
	private void hookDraggablePanelListeners() {
		mDraggablePanel.setDraggableListener(new DraggableListener() {
			@Override
			public void onMaximized() {
				Log.i(TAG, " video maximum ");
//				((MainActivity)mContext).getActionBar().hide();
				play();
			}

			@Override
			public void onMinimized() {
				// Empty
				Log.i(TAG, " video minimum ");
//				((MainActivity)mContext).getActionBar().show();
			}

			@Override
			public void onClosedToLeft() {
				Log.i(TAG, " video close to the left ");
				pause();
			}

			@Override
			public void onClosedToRight() {
				Log.i(TAG, " video close to the right ");
				pause();
			}
		});
	}

	public  void pause() {
		if (mYoutubePlayer != null) if (mYoutubePlayer.isPlaying()) {
			mYoutubePlayer.pause();
		}
	}

	public void play() {
		if (mYoutubePlayer != null) if (!mYoutubePlayer.isPlaying()) {
			mYoutubePlayer.play();
		}
	}
}

